import api from '@/api'
import { deepCopy, setPage } from '@/utils/util'

const initPage = {
    size: 10,
    number: 0,
    total: 0,
    pages: 0,
    filter: {
        enabled: null,
        title: null
    }
}

const state = {
    data: {
        logs: []
    },
    ui: {
        page: deepCopy(initPage)
    }
}

const getters = {
    logs: state => state.data.logs,

    logsTotal: state => state.ui.page.total
}

// actions
const actions = {
    getLogs({ state, commit }, params) {
        api.getLogs({
            loading: true,
            method: 'post',
            data: params ? params : state.ui.page,
            success: res => {
                const { pages, total } = res
                commit('SETLOGPAGE', { pages, total })

                let logs = res.content ? res.content : []
                commit('SETLOGS', { logs })
            }
        })
    },

    removeLog({ dispatch }, params) {
        api.removeLog({
            method: 'post',
            data: params,
            success: res => {
                dispatch('getLogs')
            }
        })
    }
}
// mutations
const mutations = {
    SETLOGS(state, payload) {
        state.data.logs = payload.logs
    },

    SETLOGPAGE(state, page) {
        page && setPage(state.ui.page, page)
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}
